<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\model\AuthRule;
use app\model\System;
use leruge\facade\Auth;

class Home
{
    // 框架
    public function wdlFrame()
    {
        return view('index');
    }

    // 首页
    public function wdlHome()
    {
        return view('home');
    }

    // 左侧菜单列表
    public function wdlLeftMenuList()
    {
        // 首页信息
        $homeInfo = [
            'title' => '首页',
            'href' => (string)url('home/wdlHome')
        ];

        // logo信息
        $systemInfo = System::order('id', 'desc')->find();
        $logoInfo = [
            'title' => $systemInfo['web_name'],
            'image' => $systemInfo['logo'],
            'href' => ''
        ];
        $userRuleIdArray = Auth::userRuleIdList(session('adminId'));
        $menuList = AuthRule::field(['id', 'pid', 'title', 'icon', 'name' => 'href'])
            ->where('id', 'in', $userRuleIdArray)
            ->where('is_show', 1)->order('sort', 'asc')->withAttr([
            'target' => function ()
            {
                return '_self';
            },
            'icon' => function ($value)
            {
                return 'fa ' . $value;
            }
        ])->select()->append(['target'])->toArray();
        $menuList = $this->buildMenuList(0, $menuList);
        // 菜单信息
        $menuInfo = $menuList;
        $info = [
            'homeInfo' => $homeInfo,
            'logoInfo' => $logoInfo,
            'menuInfo' => $menuInfo
        ];
        return json($info);
    }

    // 无权限页面
    public function wdlNoAuth()
    {
        return view('no_auth');
    }

    // 组装菜单
    private function buildMenuList($pid, $menuList)
    {
        $treeList = [];
        foreach ($menuList as $v) {
            if ($pid == $v['pid']) {
                $v['href'] = $v['href'] ? (string)url($v['href']) : null;
                $node = $v;
                $child = $this->buildMenuList($v['id'], $menuList);
                if (!empty($child)) {
                    $node['child'] = $child;
                }
                $treeList[] = $node;
            }
        }
        return $treeList;
    }
}
